#include "../Huser/User.h"
#include "xc.h"
//void *fp(void);
char process_fla_Array[2]={normal,normal};
char alarm1_Cnt=0;
char alarm1_Fla=0;
char alarm2_Cnt=0;
char alarm2_Fla=0;

char process1_fla=0;
char process2_fla=0;

struct WG_Fla {
    char has_connect = 0;
    char has_res = 0;//
    char has_dio = 0;
};

void Prj_Init(void) {

    SysInit();
    Tmr2Init();
    FA_Ctr_Init();
    WG_Det_Init();
    Tip_Init();
}

void FA_Ctr_Init(void) {
    xFA1_Pwr(Output);
    xFA2_Pwr(Output);

    FA1_Pwr_SetLow();
    FA2_Pwr_SetLow();
}

void WG_Det_Init(void) {
    xWG1_Check(Output);
    xWG1_TP1(Input);
    xWG1_TP2(Input);
    xWG1_TP3(Input);
    xWG1_Ctr(Output);

    xWG2_Check(Output);
    xWG2_TP1(Input);
    xWG2_TP2(Input);
    xWG2_TP3(Input);
    xWG2_Ctr(Output);

}

void Tip_Init(void) {
    xFault1_Signal(Output);
    xFault2_Signal(Output);

    xYled2_Pwr(Output);
    xYled1_Pwr(Output);

    xGled1_Check(Input);
    xGled2_Check(Input);

    Yled1_Pwr_SetLow();
    Yled2_Pwr_SetLow();

    Fault2_Signal_SetHigh();
    Fault1_Signal_SetHigh();
}

void SysInit(void) {
    /*
     *Oscillator
     *WDT
     * ADC
     * RBPU 
     */
    OSCCONbits.IRCF2 = 1;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF0 = 0;

    OPTION_REGbits.nRBPU = 1; //disable PortB pull-up

}

void Tmr2Init(void)//10ms
{
    INTCONbits.GIE = 1; //global interrupt
    INTCONbits.PEIE = 1; //peripheral interrupt
    PIE1bits.TMR2IE = 1; //TMR2 Enable
    PIR1bits.TMR2IF = 0; //TMR2 interrupt flag

    T2CONbits.T2CKPS = 0b01; //TMR2 prescaler 1:4
    T2CONbits.TOUTPS = 0b1001; //TMR2 Postscaler 1:10

    TMR2 = 0; //TMR2 Counter
    PR2 = 0XFA; //target Counter

    T2CONbits.TMR2ON = 0; //TMR2 ON
}

void __interrupt() myIsr(void) {//10million second
    if (PIR1bits.TMR2IF == 1) {
        PIR1bits.TMR2IF = 0;        
        if (Cnt_500ms >= 50) 
        {
            Cnt_500ms = 0;
            Fla_500ms = 1;
            
            if(alarm1_Fla==1)
            {
                alarm1_Cnt++;
                if(alarm1_Fla>AlarmTargetCnt)
                {
                    alarm1_Cnt=0;
                    process1_fla=1;
                }
                else
                {
                    process1_fla=0;
                }
            }
            else
            {
                process1_fla=0;
                alarm1_Cnt=0;
            }
            
            if(alarm2_Fla==1)
            {
                alarm2_Cnt++;
                if(alarm2_Fla>AlarmTargetCnt)
                {
                    alarm2_Cnt=0;
                    process2_fla=1;
                }
                else
                {
                    process2_fla=0;
                }
            }
            else
            {
                process2_fla=0;
                alarm2_Cnt=0;
            }
            
        }
        else
        {
            Cnt_500ms++;
        }
    }

}

void Do_Nop(int Cnt) {
    int CntTmp = 0;
    for (CntTmp = 0; CntTmp < Cnt; CntTmp++) {
        NOP();
    }
}
void main_process(void)//after array
{
    char a=0;
    for(a=0;a<2;a++)
    {
        switch(process_fla_Array[a])
        {
            case normal:
            {
                switch(a)
                {
                    case 0:
                    {
                        Yled1_Pwr_SetHigh();
                        Fault1_Signal_SetLow();
                    }
                    case 1:
                    {
                        Yled2_Pwr_SetHigh();
                        Fault2_Signal_SetLow();
                    }
                    default:break;
                }
                break;
            }
            case fault:
            {
                switch(a)
                {
                    case 0:
                    {
                        if(Gled1_Check==1)
                        {
                            Yled1_Pwr_SetHigh();
                            Fault1_Signal_SetLow();
                        }
                        else
                        {
                            Yled1_Pwr_SetLow();
                            Fault1_Signal_SetHigh();
                        }
                    }
                    case 1:
                    {
                        if(Gled2_Check==1)
                        {
                            Yled2_Pwr_SetHigh();
                            Fault2_Signal_SetLow();
                        }
                        else
                        {
                            Yled2_Pwr_SetLow();
                            Fault2_Signal_SetHigh();
                        }
                    }
                    default:break;
                }
            }
            case alarm:
            {
                switch(a)
                {
                    case 0:
                    {
                        
                        FA1_Pwr_SetHigh();
                    }
                    case 1:
                    {
                        FA2_Pwr_SetHigh();
                    }
                    default:break;
                }
            }
            default:break;
        }
    }
}
void  channel_1(void) {
    WG_Fla WG1_Fla;
    
    WG1_Check_SetHigh(); //
    WG1_Ctr_SetLow();
    Do_Nop(Delay_Cnt_);
    if ((WG1_TP1 == 0)&&(WG1_TP2 == 1))////IS CONNECTED
    {
        WG1_Fla.has_connect = 1;
        WG1_Check_SetLow();
        Do_Nop(Delay_Cnt_);
        if ((WG1_TP1 == 1)&&(WG1_TP2 == 0))//diode
        {
            WG1_Fla.has_dio=1;
            WG1_Ctr_SetHigh();
            Do_Nop(Delay_Cnt_);
            if (WG1_TP3 == 0) 
            {
                WG1_Fla.has_res=1;
                process_fla_Array[0]=normal;
            }
            else
            {
                WG1_Fla.has_res=0;
                process_fla_Array[0]=alarm;
            }
        }
        else
        {
            
        }
    } 
    else 
    {
       WG1_Fla.has_connect = 0;
       process_fla_Array[0]=fault;       
    }
    
}

void  channel_2(void) {
    WG_Fla WG2_Fla;
    
    WG2_Check_SetHigh(); //
    WG2_Ctr_SetLow();
    Do_Nop(Delay_Cnt_);
    if ((WG2_TP1 == 0)&&(WG2_TP2 == 1))////IS CONNECTED
    {
        WG2_Fla.has_connect = 1;
        WG2_Check_SetLow();
        Do_Nop(Delay_Cnt_);
        if ((WG2_TP1 == 1)&&(WG2_TP2 == 0))//diode
        {
            WG2_Fla.has_dio=1;
            WG2_Ctr_SetHigh();
            Do_Nop(Delay_Cnt_);
            if (WG2_TP3 == 0) 
            {
                WG2_Fla.has_res=1;
                process_fla_Array[1]=normal;
            }
            else
            {
                WG2_Fla.has_res=0;
                process_fla_Array[1]=alarm;
            }
        }
        else
        {
            
        }
    } 
    else 
    {
       WG2_Fla.has_connect = 0;
       process_fla_Array[1]=fault;       
    }
    
}


